package com.person.common;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.beans.factory.InitializingBean;
import com.person.ntv.dao.sys.SysResourceMapper;
import com.person.ntv.entity.sys.SysCode;
import com.person.ntv.entity.sys.SysProperty;
import com.person.ntv.service.sys.CommonService;
import com.person.ntv.service.sys.SysPropertyService;
import com.person.ntv.shiro.util.ShiroFilterChainManager;
import com.person.ntv.shiro.util.UrlFilter;

public class InitApplication  implements InitializingBean{
	@Resource
    private CommonService commonService;
    @Resource
    private SysPropertyService sysPropertyService;
    @Resource
    private SysResourceMapper sysResourceMapper;
    @Resource
	private ShiroFilterChainManager shiroFilterChainManager;
    
    public static Map<String,List<SysCode>> codeMap;
    
    public static Map<String,UrlFilter>urlMap=new HashMap<String,UrlFilter>();
    
    public static List<SysProperty>sysProperties;
    
	@Override
	public void afterPropertiesSet() throws Exception {	
		//加载所有 数据字典
		InitApplication.codeMap=codeSetList();
		//加载系统属性
		sysProperties=sysPropertyService.getSysAllProperties();
		//加载数据库权限配置
		initUrlFilter();
	}
	private void initUrlFilter(){
		try {
			List<UrlFilter> list=sysResourceMapper.getAllUrlFilter();
			shiroFilterChainManager.initFilterChains(list);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private  Map<String,List<SysCode>>codeSetList()throws Exception {
		List<Map<String,Object>> list = commonService.getCodeSetList();
		Map<String,List<SysCode>> data=new HashMap<String,List<SysCode>>();
		SysCode code=null;
		List<SysCode> codeList=null;
		for(Map<String,Object>map:list){
			codeList=data.get(String.valueOf(map.get("enName")));
			if(codeList==null){
				codeList=new ArrayList<SysCode>();
			}
			code=new SysCode();
			code.setId(new BigDecimal(String.valueOf(map.get("id"))));
			code.setItemName(String.valueOf(map.get("itemName")).trim());
			code.setItemValue(String.valueOf(map.get("itemValue")).trim());
			code.setParentItemId(String.valueOf(map.get("parentItemId")));
			codeList.add(code);
			data.put(String.valueOf(map.get("enName")),codeList);
		}
		return data;
	}
}
